﻿using System;
using System.Collections.Generic;
using System.Text;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;

namespace CMS.Data
{
    public class PhongBanDao : AbstractNHibernateDao<PhongBan, System.String>, IPhongBanDao
    {
        #region Create Instance
        private static PhongBanDao instance = null;
        public static PhongBanDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PhongBanDao();
                }
                return instance;
            }
        }
        #endregion


        #region IPhongBanDao Members

        /// <summary>
        /// Lấy danh sách người sử dụng của phòng ban
        /// </summary>
        /// <param name="maPhongBan"></param>
        /// <returns></returns>
        public List<NguoiSuDung> GetListNSDOfPhongBan(String maPhongBan)
        {
            String strHQL = @"From NguoiSuDung nsd Where nsd.MaPhongBan = '" + maPhongBan + "' And nsd.TrangThai=1 Order By nsd.ThuTu, nsd.Ten, nsd.HoDem";
            return NguoiSuDungDao.Instance.GetHQLListT(strHQL);
        }

        public byte GetMaxSTT()
        {
            byte result = 1;
            try
            {
                Object obj = null;
                String strSQL = @"Select Max(o.ThuTu)from PhongBan o ";
                obj = NHibernateSessionManager.Instance.GetSession().CreateQuery(strSQL)
                    .UniqueResult();
                if (obj != null)
                {
                    result = Convert.ToByte(obj);
                    result += 1;
                }
                return result;
            }
            catch
            {
                return result;
            }
        }

        //public PhongBan GetPhongBanOfVietTat(string valueInput)
        //{
        //    try
        //    {
        //        PhongBan phongban = null;
        //        List<PhongBan> listphongban = this.GetFieldEqualCriteria("TenPhongBan", valueInput);
        //        if (listphongban != null && listphongban.Count > 0)
        //        {
        //            phongban = listphongban[0];
        //        }
        //        else
        //        {
        //            listphongban = this.GetFieldEqualCriteria("VietTat", valueInput);
        //            if (listphongban != null && listphongban.Count > 0)
        //                phongban = listphongban[0];
        //        }
        //        return phongban;
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //}
     
        public List<PhongBan> GetAllHasOrderBy(bool TrangThai)
        {
            try
            {
                String strQuery = @"From PhongBan o Where o.TrangThai =:TrangThai Order by o.ThuTu, o.TenPhongBan";
                List<PhongBan> Result = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                        .SetParameter("TrangThai", TrangThai)
                        .List<PhongBan>() as List<PhongBan>;
                return Result;
            }
            catch
            {
                return new List<PhongBan>();
            }  
        }

        //public NguoiSuDung GetNguoiDaiDienOfPhongBan(String MaPhongBan)
        //{
        //    try
        //    {
        //        NguoiSuDung Result = null;
        //    PhongBan phongban = this.GetById(MaPhongBan, true);
        //    if (phongban != null && !phongban.NguoiDaiDien.Equals(String.Empty))
        //        Result = NguoiSuDungDao.Instance.GetById(phongban.NguoiDaiDien, true);
        //    return Result;

        //    }catch 
        //    {
        //        return null;
        //    }
        //}

        #endregion
    }
}
